import {
  UserLogin,
  GetInfo
} from '@/api/user'
import {
  Toast
} from 'vant'

export default {
  namespaced: true,
  state: {
    userInfo: {}
  },
  mutations: {
    SET_USER(state, data) {
      state.userInfo = data
    }
  },
  actions: {
    login({
      commit
    }, {
      vm,
      username,
      password,
    }) {
      UserLogin({
          username,
          password
        })
        .then(async res => {
          // eslint-disable-next-line no-console
          console.log(res)
          window.localStorage.setItem('uuid', username)
          window.localStorage.setItem('token', res)
          let userInfo = await GetInfo()
          await commit('SET_USER', userInfo)
          vm.$router.push('/home')
          Toast('登陆成功')
        })
    }
  }
}